#ifndef  __UNITREADER_H_
#define  __UNITREADER_H_

#include "common.h"
#include "ini_file_reader.h"
#include "logger.h"
#include "hash.h"
#include "descriptorManager.h"
#include "termlist.h"
#include <google/dense_hash_map>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#define MAX_UNIT_FNUM	1024

//example:
//name:val1
//Unit* get_unit_by_value("val1");
//unit->get_content_by_id(64);:w


typedef struct _IdxItem  {
  uint32_t* fno;
  uint32_t* off;
  uint32_t* len;
} IdxItem;

class UnitReader {
public:
  UnitReader() {};
  ~UnitReader() {};
  
  int load(DescriptorManager *dm, char* data_path);

  int get_rsc_id(char* rsc_name) {
    return m_dm->get_rsc_id_by_name(rsc_name);
  }

  char* get_by_id(int32_t id, uint32_t rsc_id,
      uint32_t* len) {
//    NOTICE("id[%u] rsc_id[%u] fno[%u] off[%u] len[%u]",
//	id, rsc_id,
//	items[id].fno[rsc_id], items[id].off[rsc_id], items[id].len[rsc_id]);

    *len = items[id].len[rsc_id];
    return m_mapptr[items[id].fno[rsc_id]] + items[id].off[rsc_id];
  }
private:
  int load_mmap();
  int load_idx();

  int m_unit_fnum;
  uint32_t m_rsc_num;
  uint32_t m_link_num;

  IdxItem* items;

  DescriptorManager* m_dm;
  char m_data_path[NAME_LEN];
  char* m_mapptr[MAX_UNIT_FNUM];
};

#endif
